package com.orcsun.lucy8.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Common utility methods
 * @author Richard (orcfeng@gmail.com)
 * @since 1.0
 */
public class LucyUtil {

	/**
	 * Return "yyyy-mm-dd"
	 * @return
	 */
	public static String getToday(){
		Date d = new Date();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		return f.format(d);
	}
	
	/**
	 * 
	 * @param startDate
	 * @return
	 * @throws ParseException
	 */
	public static String getNextPayDay(String startDate) throws ParseException{
		return getDate(startDate,1);
	}
	
	/**
	 * 
	 * @param startDate
	 * @return
	 * @throws ParseException
	 */
	public static String getPrePayDay(String startDate) throws ParseException{
		return getDate(startDate,-1);
	}
	
	public static String getDate(String date,int months) throws ParseException{
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		Date d = f.parse(date);
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.MONTH, months);
		return f.format(c.getTime());
	}
		
	/**
	 * 
	 * @param sdate
	 * @param edays
	 * @return
	 * @throws ParseException 
	 */
	public static int getDays(String sdate,String edate) throws ParseException{

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		Date sd = f.parse(sdate);
		Date ed = f.parse(edate);
		long sdl = sd.getTime();
		long edl = ed.getTime();
		long oneday = 24*60*60*1000;
		
		return (int)((edl-sdl)/(oneday));
	}
	
	public static void main(String[] args){
		try {
			System.out.println(getDays("2012-03-21","2012-04-2"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
